// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

axios.defaults.baseURL = "http://localhost:8080"

//设置token携带到后端
axios.interceptors.request.use(req => {
  let token = localStorage.getItem("token")
  if (token) {
    req.headers["token"] = token
  }
  return req
})
//设置后置拦截器
axios.interceptors.response.use(resp => {
  if (resp.headers["token"]) {
    localStorage.setItem("token",resp.headers["token"])
  }
  return resp
})

//利用路由守卫做登录校验
router.beforeEach((to, from, next) => {
  let path = to.path
  if (path == "/login") {
    next()
  } else {
    let token = localStorage.getItem("token")
    if (token) {
      next()
    } else {
      next("/login")
    }
  }
})


Vue.prototype.$http = axios

Vue.use(ElementUI)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
